//
//  YTHomeTitleView.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/10.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHomeTitleView.h"
@interface YTHomeTitleView ()

//@property(nonatomic ,strong)NSArray *fakeList;

@end

@implementation YTHomeTitleView



- (void)awakeFromNib
{
    [super awakeFromNib];

}

// 分类的按钮监听
- (void)buttonClick:(YTDHButton *)sender
{
    NSAssert(self.buttonClick != nil, @"buttonClick 不能为nil");
    
    self.buttonClick(sender.tag);
}

- (void)setCateList:(NSArray<YTHomeCate *> *)cateList
{
    _cateList = cateList;
    
    [self setupUI];
}

// 设置界面
- (void)setupUI
{
    // 每个按钮的宽度
    CGFloat btnW = kScreenWidth / 5;
    
    // 遍历创建子控件
    for (int i = 0; i < self.cateList.count; i++) {
        CGRect fra = CGRectMake(i * btnW, 0, btnW, self.bounds.size.height);
        
        YTDHButton *btn = [YTDHButton dh_buttonWithFrame:fra title:self.cateList[i].name selectedColor:UIColor.redColor];
        
        btn.selected = (i == 0) ? YES : NO;
        btn.tag = i;
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
    }
    
    self.contentSize = CGSizeMake(self.cateList.count * btnW, 0);

}
@end
